package com.bysj.yqsht.service.impl;

import com.bysj.yqsht.bean.CheckStudent;
import com.bysj.yqsht.bean.CheckStudentExample;
import com.bysj.yqsht.bean.CheckallStudent;
import com.bysj.yqsht.bean.Student;
import com.bysj.yqsht.bean.extend.CheckStudentExtend;
import com.bysj.yqsht.dao.CheckStudentMapper;
import com.bysj.yqsht.dao.CheckallStudentMapper;
import com.bysj.yqsht.dao.StudentMapper;
import com.bysj.yqsht.dao.extend.CheckStudentExtendMapper;
import com.bysj.yqsht.dao.extend.CheckallStudentExtendMapper;
import com.bysj.yqsht.service.ICheckStudentService;
import com.bysj.yqsht.utils.CustomerException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ICheckStudentServiceImpl implements ICheckStudentService {
    @Resource
    private CheckStudentMapper checkStudentMapper;
    @Resource
    private CheckStudentExtendMapper checkStudentExtendMapper;
    @Resource
    private CheckallStudentMapper checkallStudentMapper;
    @Resource
    private CheckallStudentExtendMapper checkallStudentExtendMapper;
    @Resource
    private StudentMapper studentMapper;

    @Override
    public List<CheckStudent> findAll() {
        CheckStudentExample example = new CheckStudentExample();
        return checkStudentMapper.selectByExample(example);
    }

    @Override
    public void saveOrUpdate(CheckStudent checkStudent) {
    Student s1 = studentMapper.selectByPrimaryKey(checkStudent.getStudentId());
    if(s1.getClassId().equals(checkStudent.getClassId())){
     //   checkStudentMapper.insert(checkStudent);
        CheckallStudent c1=  checkallStudentExtendMapper.findWithAllByStudentId(checkStudent.getStudentId());
//        判断是否存在该ID的学生信
        CheckallStudent checkallStudent = new CheckallStudent();
        checkallStudent.setStudentId(checkStudent.getStudentId());
        checkallStudent.setClassesId(checkStudent.getClassId());
        if (c1 != null){
            checkallStudent.setId(c1.getId());
        }
        if(c1 == null){
//        如果查询结果为NULL 说明不存在该学生 则给该学生赋值插入
//           创建checkall对象
            checkallStudent.setNumber("1");
           checkallStudentMapper.insert(checkallStudent);
        }else{
            Integer num = Integer.parseInt(c1.getNumber());
            num++;
            checkallStudent.setNumber(num.toString());
            checkallStudentMapper.updateByPrimaryKey(checkallStudent);
        }
        if(checkStudent.getId() != null){
            checkStudentMapper.updateByPrimaryKey(checkStudent);
        } else {
            checkStudentMapper.insert(checkStudent);
        }
    }else{
        throw  new CustomerException(" 选择的学生和他所在的班级不一致，请审核！");
    }

    }

    @Override
    public void deleteById(long id) {
        checkStudentMapper.deleteByPrimaryKey(id);

    }

    @Override
    public List<CheckStudentExtend> findWithAll(int pageNum,int limit) {
        PageHelper.startPage(pageNum,limit);
        CheckStudentExample checkStudentExample = new CheckStudentExample();
        List<CheckStudentExtend> list1 = checkStudentExtendMapper.findWithAll();
        PageInfo<CheckStudentExtend> data = new PageInfo<CheckStudentExtend>(list1);
        List<CheckStudentExtend> list = data.getList();
        return list;
    }

    @Override
    public List<CheckStudentExtend> findWithAllByStudent(Long student,Long classesId,String type) {
        return checkStudentExtendMapper.findWithAllByStudent(student,classesId,type);
    }
}
